package com.example.content.adapter.recycle

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.example.content.R

/**
 * Created by zhudongao
 * Desc
 * Date 2020/11/4
 */
class SimpleListAdapter(val context: Context) : BaseAdapter() {

    val data = mutableListOf<String>()
    var selectPosition = -1;

    override fun getCount() = data.size

    override fun getItem(position: Int): String {
        return if (checkPosition(position)) data[position] else ""
    }

    private fun checkPosition(position: Int): Boolean {
        return position >= 0 && position <= data.size - 1
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
        val holder: ViewHolder
        val holderView: View
        if (convertView == null) {
            holderView = View.inflate(context, R.layout.drop_down_list_item, null)
            holder = ViewHolder(holderView)
            holderView.tag = holder
        } else {
            holderView = convertView
            holder = convertView.tag as ViewHolder
        }
        val data = getItem(position)
        if (!data.isBlank()) {
            holder.text?.text = data
            if (selectPosition != -1) {
                holder.text?.isSelected = selectPosition == position
            }
        }
        return holderView
    }

    inner class ViewHolder(view: View) {
        var text: TextView? = null

        init {
            text = view.findViewById(R.id.text)
        }
    }
}